package com.vegetable.modules.controller.baseData;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.PageUtils;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.baseData.FundAccount;
import com.vegetable.modules.entity.baseData.FundAccountDetail;
import com.vegetable.modules.entity.baseData.po.FundAccountDetailPO;
import com.vegetable.modules.service.baseData.IFundAccountDetailService;
import com.vegetable.modules.service.baseData.IFundAccountService;
import com.vegetable.modules.service.outbound.ISalesOutService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 资金账户 前端控制器
 * </p>
 *
 * @author wangruijie
 * @since 2024-03-04
 */
@Slf4j
@RestController
@RequestMapping("/fundAccount")
@AllArgsConstructor
@Api(value = "资金账户", tags = "资金账户")
public class FundAccountController extends AbstractController {

    private IFundAccountService fundAccountService;

    private IFundAccountDetailService iFundAccountDetailService;

    private ISalesOutService salesOutService;
    /**
     * 所有资金账户列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "条件查询资金账户列表", notes = "条件查询资金账户列表")
    public R list(@RequestParam Map<String, Object> params){
        log.info("条件查询资金账户列表{}", JSON.toJSONString(params));
        PageUtils page = fundAccountService.queryPage(params);

        return R.data( page);
    }
    @PostMapping("/showList")
    @ApiOperation(value = "查询资金出入记录", notes = "查询资金出入记录")
    public R showList(@RequestBody FundAccountDetailPO po){
        log.info("查询资金出入记录{}", JSON.toJSONString(po));
        IPage<FundAccountDetail> page = iFundAccountDetailService.detailList(po);
        return R.data( page);
    }

    /**
     * 资金账户信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "资金账户信息", notes = "资金账户信息")
    public R info(@PathVariable("id") Long id){
        FundAccount fundAccount = fundAccountService.getById(id);
        return R.data(fundAccount);
    }

    /**
     * 保存资金账户
     */
    @SysLog(value = "保存资金账户")
    @PostMapping("/save")
    @ApiOperation(value = "保存资金账户", notes = "保存资金账户")
    public R save(@RequestBody FundAccount fundAccount){
        fundAccount.setCreateUserId(getUserId());
        fundAccountService.save(fundAccount);
        return R.status(true);
    }

    /**
     * 修改资金账户
     */
    @SysLog(value = "修改资金账户")
    @PostMapping("/update")
    @ApiOperation(value = "修改资金账户", notes = "修改资金账户")
    public R update(@RequestBody FundAccount fundAccount){
        fundAccount.setCreateUserId(getUserId());
        fundAccountService.updateById(fundAccount);
        return R.status(true);
    }

    /**
     * 删除资金账户
     */
    @SysLog(value = "删除资金账户")
    @GetMapping("/delete")
    @ApiOperation(value = "删除资金账户", notes = "删除资金账户")
    public R delete(Long id){
        FundAccount fundAccount = fundAccountService.getById(id);
        if (fundAccount == null){
            return R.fail("数据不存在");
        }
        fundAccount.setIsDel(1);
        fundAccount.setCreateUserId(getUserId());
        fundAccountService.updateById(fundAccount);
        return R.success("请联系专人清理");
    }

    @SysLog(value = "财务收支分析")
    @GetMapping("/financialAnalysis")
    @ApiOperation(value = "财务收支分析", notes = "财务收支分析")
    public R financialAnalysis(Integer chartRange){
        log.info("统计近{}个月的财务数据", chartRange);
        return fundAccountService.financialAnalysis(chartRange);
    }

}
